package web;

import com.google.gson.Gson;
import pojo.Book;
import pojo.Cart;
import pojo.CartItem;
import service.BookService;
import service.impl.BookServiceImpl;
import utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CartServlet extends BaseServlet {
    private BookService book = new BookServiceImpl();

//    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //获取请求的编号
//        String id = req.getParameter("id");
//        //在数据库中查找该图书信息
//        Book book = this.book.queryBookById(WebUtils.paraseInt(id, 0));
//        //将book对象变成购物车对象
//        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
//        //获取当前网页的cart对象
//        Cart cart = (Cart) req.getSession().getAttribute("cart");
//        //判断当前网页中的session中的cart对象是否为空
//        if (cart == null) {
//            cart = new Cart();
//            req.getSession().setAttribute("cart", cart);
//        }
//        cart.addItem(cartItem);
//        //回响您刚刚将xxx添加进购物车 重定向不支持request域共享数据，所以用session
//        req.getSession().setAttribute("lastName",cartItem.getName());
//        resp.sendRedirect(req.getHeader("Referer"));
//    }

    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取要删除的商品的id
        String id = req.getParameter("id");
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.deleteItemById(WebUtils.paraseInt(id, 0));
            resp.sendRedirect(req.getHeader("Referer"));
            System.out.println(cart);
        }
    }

    protected void clearItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.clearCart();
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
    protected void updateItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.paraseInt(req.getParameter("id"), 0);
        int count = WebUtils.paraseInt(req.getParameter("count"), 1);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.updateCount(id,count);
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
    protected void ajaxShow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        int id = WebUtils.paraseInt(req.getParameter("id"), 0);
        Book book = this.book.queryBookById(id);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        cart.addItem(cartItem);
        req.getSession().setAttribute("lastName",cartItem.getName());
        Map<String,Object>map = new HashMap<>();
        map.put("num",cart.getTotalCount());
        map.put("lastName",cartItem.getName());
        Gson gson = new Gson();
        String s = gson.toJson(map);
        System.out.println(s);
        resp.getWriter().write(s);
    }
}